<!DOCTYPE html>
<html lang="zh_CN" xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"
      xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
    <meta charset="utf-8"/>
    <title>用户管理列表 User List</title>
    <meta name="renderer" content="webkit"/>
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"/>
    <meta name="viewport"
          content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0"/>
    <link rel="stylesheet" th:href="@{/statics/layui/css/layui.css}" media="all"/>
    <link rel="stylesheet" th:href="@{/statics/style/admin.css}" media="all"/>
</head>
<body>

<div class="layui-fluid">
    <div class="layui-row layui-col-space15">
        <div class="layui-col-md12">
            <div class="layui-card">
                <div class="layui-form layui-card-header layuiadmin-card-header-auto">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">用户名</label>
                            <div class="layui-input-block" style="width: 193px;">
                                <input class="layui-input" name="userName" id="layui-form-user-name" autocomplete="off"
                                       th:value="${userName}">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">邮箱</label>
                            <div class="layui-input-block" style="width: 193px;">
                                <input class="layui-input" name="email" id="layui-form-email" autocomplete="off"
                                       th:value="${email}">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">手机号码</label>
                            <div class="layui-input-block" style="width: 193px;">
                                <input class="layui-input" name="mobile" id="layui-form-mobile" autocomplete="off"
                                       th:value="${mobile}">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">角色</label>
                            <div class="layui-input-block" style="width: 193px;">
                                <select name="roleId" lay-verify="" id="layui-form-role-id">
                                    <option value="">全部角色</option>
                                    <option th:each="r:${roles}" th:value="${r.id}" th:text="${r.name}"
                                            th:selected="${r.id == roleId}"></option>
                                </select>
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">状态</label>
                            <div class="layui-input-block" style="width: 193px;">
                                <select name="state" lay-verify="" id="layui-form-state">
                                    <option value="">全部状态</option>
                                    <option value="1" th:selected="${state == '1'}">正常</option>
                                    <option value="9" th:selected="${state == '9'}">锁定</option>
                                </select>
                            </div>
                        </div>
                        <button class="layui-btn" data-type="search">搜索</button>
                    </div>
                </div>
                <div class="layui-card-body">

                    <table class="layui-hide" id="user-list-table" lay-filter="user-list-filter"></table>

                    <script type="text/html" id="user-list-table-operate-bar">
                        <a shiro:hasPermission="sys:user:edit" class="layui-btn layui-btn-xs"
                           lay-event="edit"><i class="layui-icon layui-icon-edit"></i>修改</a>
                        <a shiro:hasPermission="sys:user:remove" class="layui-btn layui-btn-danger layui-btn-xs"
                           lay-event="del"><i class="layui-icon layui-icon-delete"></i>删除</a>
                    </script>

                    <script type="text/html" id="user-list-table-toolbar">
                        <div class="layui-btn-container">
                            <button shiro:hasPermission="sys:user:add" class="layui-btn layui-btn-sm" lay-event="add">
                                添加新用户
                            </button>
                        </div>
                    </script>

                </div>
            </div>
        </div>
    </div>
</div>


<script th:src="@{/statics/layui/layui.js}"></script>
<script th:inline="none">
    layui.config({
        base: '/statics/' //静态资源所在路径
    }).extend({
        index: 'lib/index' //主入口模块
    }).use(['index', 'table'], function () {
        var $ = layui.$
            , admin = layui.admin
            , table = layui.table;

        var tableIns = table.render({
            elem: '#user-list-table'
            , url: '/api/user/page'
            , id: 'id'
            , toolbar: '#user-list-table-toolbar'

            , cols: [[
                {field: 'id', width: 60, title: 'ID'}
                , {field: 'userName', width: 150, title: '用户名', sort: true}
                , {field: 'avatar', width: 60, title: '头像', templet:function (d) {
                    if(d.avatar){
                        return '<img style="display: inline-block; width: 100%; height: 100%;" src="'+d.avatar+'"/>\n';
                    } else {
                        return '<img style="display: inline-block; width: 100%; height: 100%;" src="/images/default-avatar.jpg"/>\n';
                    }
                }}
                , {field: 'nickName', width: 150, title: '昵称'}
                , {field: 'email', width: 150, title: '邮箱'}
                , {field: 'mobile', width: 150, title: '手机'}
                , {field: 'roleName', width: 150, title: '角色名称'}
                , {field: 'lastLoginIp', width: 180, title: '最后登录IP', sort: true}
                , {field: 'lastLoginTime', width: 180, title: '最后登录时间', sort: true}
                , {field: 'loginCount', width: 180, title: '登录次数', sort: true}
                , {field: 'createTime', width: 180, title: '加入时间', sort: true}
                , {field: 'updateTime', width: 180, title: '更新时间', sort: true}
                , {
                    field: 'state', width: 80, title: '状态', fixed: 'right', sort: true, templet: function (d) {
                        if (d.state == '1') {
                            return '<button class="layui-btn layui-btn-xs" title="点击锁定">正常</button>';
                        } else {
                            return '<button class="layui-btn layui-btn-primary layui-btn-xs"title="点击解锁">锁定</button>';
                        }
                    }
                }
                , {width: 150, align: 'center', fixed: 'right', toolbar: '#user-list-table-operate-bar'}
            ]]
            , page: true
            , request: {
                pageName: 'pageNum'
                , limitName: 'pageSize'
            }
        });

        //头工具栏事件
        table.on('toolbar(user-list-filter)', function (obj) {
            var checkStatus = table.checkStatus('id');
            switch (obj.event) {
                case 'add':
                    layer.open({
                        type: 2
                        , title: '添加用户'
                        , content: '/web/user/add'
                        , maxmin: true
                        , area: ['500px', '450px']
                        , btn: ['确定', '取消']
                        , yes: function (index, layero) {
                            var iframeWindow = window['layui-layer-iframe' + index]
                                , submitID = 'LAY-user-add-submit'
                                , submit = layero.find('iframe').contents().find('#' + submitID);

                            //监听提交
                            iframeWindow.layui.form.on('submit(' + submitID + ')', function (data) {
                                var field = data.field; //获取提交的字段

                                //提交 Ajax 成功后，静态更新表格中的数据
                                $.ajax({
                                    url: '/api/user/add',
                                    method: 'post',
                                    data: data.field,
                                    dataType: 'JSON',
                                    success: function (res) {
                                        if (res.code == '0') {
                                            tableIns.reload({page: {curr: 1}}); //数据刷新
                                            layer.close(index); //关闭弹层
                                        } else {
                                            alert(res.msg);
                                        }
                                    },
                                    error: function (err) {
                                    }
                                });

                            });

                            submit.trigger('click');
                        }
                    });
                    break;
            }
            ;
        });

        //监听行工具事件
        table.on('tool(user-list-filter)', function (obj) {
            var data = obj.data;
            switch (obj.event) {
                case 'del':
                    layer.confirm('真的删除么', function (index) {
                        $.ajax({
                            url: '/api/user/remove?id=' + data.id,
                            method: 'delete',
                            contentType: "application/json;charset=UTF-8",
                            dataType: 'JSON',
                            success: function (res) {
                                if (res.code == '0') {
                                    layer.msg('记录已删除!', {icon: 1, time: 1000}, function () {
                                        obj.del();
                                        layer.close(index); //关闭弹层
                                    });
                                } else {
                                    layer.msg(res.msg);
                                }
                            },
                            error: function (err) {
                            }
                        });
                    });
                    break;
                case 'edit':
                    layer.open({
                        type: 2
                        , title: '修改用户'
                        , content: '/web/user/edit?id=' + data.id
                        , maxmin: true
                        , area: ['500px', '450px']
                        , btn: ['确定', '取消']
                        , yes: function (index, layero) {
                            var iframeWindow = window['layui-layer-iframe' + index]
                                , submitID = 'LAY-user-add-submit'
                                , submit = layero.find('iframe').contents().find('#' + submitID);

                            //监听提交
                            iframeWindow.layui.form.on('submit(' + submitID + ')', function (data) {
                                var field = data.field; //获取提交的字段

                                //提交 Ajax 成功后，静态更新表格中的数据
                                $.ajax({
                                    url: '/api/user/edit',
                                    method: 'post',
                                    data: data.field,
                                    dataType: 'JSON',
                                    success: function (res) {
                                        if (res.code == '0') {
                                            tableIns.reload({page: {curr: 1}}); //数据刷新
                                            layer.close(index); //关闭弹层
                                        } else {
                                            alert(res.msg);
                                        }
                                    },
                                    error: function (err) {
                                    }
                                });

                            });

                            submit.trigger('click');
                        }
                    });
                    break;
            }
        });

        var $ = layui.$, active = {
            search: function () {
                var userNameField = $('#layui-form-user-name');
                var emailField = $('#layui-form-email');
                var mobileField = $('#layui-form-mobile');
                var roleIdField = $('#layui-form-role-id');
                var stateField = $('#layui-form-state');

                //执行重载
                tableIns.reload({
                    page: {
                        curr: 1 //重新从第 1 页开始
                    }
                    , where: {
                        email: emailField.val(),
                        mobile: mobileField.val(),
                        roleId: roleIdField.val(),
                        state: stateField.val(),
                        userName: userNameField.val()
                    }
                });
            }
        };
        $('.layui-form .layui-form-item .layui-btn').on('click', function () {
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });

    });
</script>
</body>
</html>